package com.tanhua.manage.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.tanhua.manage.mapper.RoleMapper;
import com.tanhua.manage.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author Au
 * @DATE 2020/12/13 19:01
 **/
@Service
public class RoleService {

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 查询角色列表
     *
     * @return
     */
    public List<Role> queryRole() {
        return roleMapper.findAll();
    }

    /**
     * 删除角色
     *
     * @param id
     * @return
     */
    public boolean deleteRoleById(Long id) {
        //先删除关系表r
        int result = delete(id);

        int result1 = roleMapper.deleteRoleById(id);

        return result > 0 && result1 > 0;
    }

    /**
     * 保存角色
     *
     * @param roleName
     * @param roleDesc
     * @param state
     * @param permissionIds
     * @return
     */
    public boolean saveRole(String roleName, String roleDesc, Integer state, Integer[] permissionIds) {
        Role role = new Role();
        role.setRoleName(roleName);
        role.setRoleDesc(roleDesc);
        role.setState(state);
        int result = roleMapper.insert(role);

        Long roleId = role.getId();

        // 关系表中插入字段
        saveRelation(roleId, permissionIds);

        return result > 0;
    }

    /**
     * 更新角色
     *
     * @param roleId
     * @param permissionIds
     * @return
     */
    public boolean updateRole(Long roleId, Integer[] permissionIds) {
        // 先删除关系表中所有关联的字段
        int result = delete(roleId);

        //再关系表中新增字段
        saveRelation(roleId, permissionIds);

        return result > 0;
    }

    // 先删除关系表中所有关联的字段
    public int delete(Long roleId) {
        return roleMapper.deleteRoleAndPermissionByRoleId(roleId);
    }

    //再关系表中新增字段
    public void saveRelation(Long roleId, Integer[] permissionIds) {
        for (Integer permissionId : permissionIds) {
            roleMapper.saveRoleIdAndPermissionId(roleId, permissionId);
        }
    }


    /**
     * 更新状态
     *
     * @param roleId
     * @param state
     * @return
     */
    public boolean updateState(Long roleId, Integer state) {
        state = state == 1 ? 1 : 2;

        Role role = roleMapper.selectById(roleId);

        LambdaUpdateWrapper<Role> UpdateWrapper = new LambdaUpdateWrapper();
        UpdateWrapper.set(Role::getState, state);
        UpdateWrapper.eq(Role::getId, roleId);

        int result = roleMapper.update(role, UpdateWrapper);

        return result > 0;
    }

    public List<Role> queryRoleByRoleName(String roleName) {
        Wrapper<Role> queryWrapper = new QueryWrapper<>();
        ((QueryWrapper<Role>) queryWrapper)
                .like("role_name", roleName).orderByAsc("id");
        List<Role> roles = roleMapper.selectList(queryWrapper);
        return roles;

    }
}
